package com.aeye.mbr.upms.rpc.service.impl;

import com.aeye.mbr.common.base.constants.ErrorCodeConstants;
import com.aeye.mbr.common.exception.ServiceException;
import com.aeye.mbr.common.plugin.sequence.SequenceGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.result.BootstrapTableResult;
import com.aeye.mbr.common.plugin.mapper.BaseServiceImpl;
import com.aeye.mbr.upms.rpc.mapper.UpmsOrganizationMapper;
import com.aeye.mbr.upms.dao.model.UpmsOrganization;
import com.aeye.mbr.upms.rpc.api.UpmsOrganizationService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * UpmsOrganizationService实现
 * Created by shuaeye.mbr on 2017/3/20.
 */
@Service
@Transactional
public class UpmsOrganizationServiceImpl extends BaseServiceImpl<UpmsOrganization> implements UpmsOrganizationService {

    private static Logger _log = LoggerFactory.getLogger(UpmsOrganizationServiceImpl.class);

    @Autowired
    UpmsOrganizationMapper upmsOrganizationMapper;

    @Override
    public BootstrapTableResult<UpmsOrganization> selectUpmsOrganizationByParams(BootstrapTableParams params,String parentCode) {
        BootstrapTableResult result = new BootstrapTableResult();
        if (params != null) {
            PageHelper.startPage(params.getPage(), params.getLimit());
            Example example = new Example(UpmsOrganization.class);
            Example.Criteria criteria = example.createCriteria();
            String sort = params.getSort();
            String order = params.getOrder();
            if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
                example.setOrderByClause(sort + " " + order);
            }
            String search = params.getSearch();
            if (StringUtils.isNotBlank(search)) {
                example.or()
                        .andLike("name", "%" + search + "%");
            }
            if(StringUtils.isNotBlank(parentCode)){
                example.and().andLike("path","%|"+parentCode+"|%");
            }
            List<UpmsOrganization> permissions = upmsOrganizationMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(permissions)) {
                if (permissions instanceof Page) {
                    Page<UpmsOrganization> pls = (Page) permissions;
                    result.setTotal((int) pls.getTotal());
                    result.setRows(pls.getResult());
                } else {
                    result.setTotal(permissions.size());
                    result.setRows(permissions);
                }
            }else{
                result.setTotal(0);
                result.setRows(Collections.emptyList());
            }
        }
        return result;
    }

    @Override
    public int addUpmsOrganization(UpmsOrganization upmsOrganization) {
        UpmsOrganization parentOrg = upmsOrganizationMapper.selectByPrimaryKey(upmsOrganization.getPid());
        if(null == parentOrg){
            throw new ServiceException(ErrorCodeConstants.FAILD,"找不到父机构");
        }
        upmsOrganization.setPath(parentOrg.getPath()+"|"+upmsOrganization.getOrganizationId()+"|");
        upmsOrganizationMapper.insert(upmsOrganization);
        return 0;
    }

    @Override
    public int deleteUpmsOrganization(String id) {
        Example example = new Example(UpmsOrganization.class);
        Example.Criteria criteria = example.createCriteria();
            example.and()
                    .andEqualTo("pid", id);
        int childrenCount = upmsOrganizationMapper.selectCountByExample(example);
        if(childrenCount > 0){
            throw new ServiceException(ErrorCodeConstants.FAILD,"机构下存在子机构");
        }
        return upmsOrganizationMapper.deleteByPrimaryKey(id);
    }
}